package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GoodsBean;
import com.project.mapper.IGoodsMapper;
import com.project.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    private IGoodsMapper goodsMapper;
    @Override
    public GoodsBean findById(Integer goodsId) {
        return goodsMapper.selectById(goodsId);
    }

    @Override
    public void add(GoodsBean goodsBean) {
        goodsMapper.insert(goodsBean);

    }

    @Override
    public void updateLastPrice(Integer goodsId, Integer newPrice) {
        GoodsBean goodsBean = goodsMapper.selectById(goodsId);
        goodsBean.setLastPrice(newPrice);
        goodsMapper.updateById(goodsBean);

    }

    @Override
    public void updateNum(Integer goodsId, Integer deltaNum) {
        GoodsBean goodsBean = goodsMapper.selectById(goodsId);
        goodsBean.setNum(goodsBean.getNum()+deltaNum);
        goodsMapper.updateById(goodsBean);
    }

    @Override
    public IPage<GoodsBean> cutFindByItem(Integer pageNum, String goodsName,
                                          Integer minNum, Integer maxNum,
                                          String isSuggestPrice, String type) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        if (goodsName != null && !goodsName.equals("")){
            qw.like("g_name",goodsName);
        }
        if (minNum != null){
            qw.ge("g_num",minNum);
        }
        if (maxNum != null){
            qw.le("g_num",maxNum);
        }
        if (isSuggestPrice != null){
            qw.ge("g_suggestPrice",0);
        }
        if (type != null){
            qw.like("g_type",type);
        }

        return goodsMapper.selectPage(new Page<>(pageNum,3),qw);
    }

    @Override
    public void updateSuggestPrice(Integer goodsId, Integer newSuggestPrice) {
        GoodsBean goodsBean = goodsMapper.selectById(goodsId);
        goodsBean.setSuggestPrice(newSuggestPrice);
        goodsMapper.updateById(goodsBean);
    }
}
